package com.xii.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xii.eduservice.entity.EduSubject;
import com.xii.eduservice.entity.excel.SubjectData;
import com.xii.eduservice.service.EduSubjectService;
import com.xii.servicebase.exceptionhandler.XiiException;

import java.util.Map;

/**
 * @description: excel上传读取监听器
 * 
 * @author  wangxihao
 * @email wangxh0108@163.com
**/
// SubjectExcelListener不能交给spring去管理  不需要  需要手动用
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    //手动写有参无参构造 给EduSubjectService赋值
    public EduSubjectService subjectService;

    public SubjectExcelListener() {}
    //创建有参数构造，传递subjectService用于操作数据库
    public SubjectExcelListener(EduSubjectService subjectService) {
        this.subjectService = subjectService;
    }


    //一行一行的读取内容
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if(subjectData == null) {
            throw new XiiException(20001,"添加失败!文件为空！");
        }

        System.out.println("数据行"+subjectData);
        //添加一级分类   判断是不是一级标题

        EduSubject existOneSubject = SubjectExcelListener.existOneSubject(subjectService,subjectData.getOneSubjectName());
        if(existOneSubject == null) {//没有相同的  添加为一级标题
            existOneSubject = new EduSubject();
            existOneSubject.setTitle(subjectData.getOneSubjectName());
            existOneSubject.setParentId("0");
            subjectService.save(existOneSubject);
        }

//        每个数据都有一级标题 获得一级分类的id
        String pid = existOneSubject.getId();

        //添加二级分类  判断添加二级标题
        EduSubject existTwoSubject = SubjectExcelListener.existTwoSubject(subjectService,subjectData.getTwoSubjectName(),pid);
        if(existTwoSubject == null) {//没有相同的  添加为一级标题
            existTwoSubject = new EduSubject();
            existTwoSubject.setTitle(subjectData.getTwoSubjectName());
            existTwoSubject.setParentId(pid);
            subjectService.save(existTwoSubject);
        }


    }

    //读取的头
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("表格"+headMap);
    }

    //读取后执行
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }


    //判断一级分类是否重复
    private static EduSubject existOneSubject(EduSubjectService subjectService,String name) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id","0");
        EduSubject eduSubject = subjectService.getOne(wrapper);
        System.out.println("查询一级分类:"+eduSubject);
        return eduSubject;
    }

    //判断二级分类是否重复
    private static EduSubject existTwoSubject(EduSubjectService subjectService,String name,String pid) {
        //MP的条件构造器
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",pid);
        EduSubject eduSubject = subjectService.getOne(wrapper);
        return eduSubject;
    }



}
